@include('admin::layouts.header')
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>商品管理</legend>
    </fieldset>
	<div class="demoTable layui-row">
		<div class="layui-form layui-form-pane" style="float:left;">
			<div class="layui-form-item">
				<label class="layui-form-label">快捷查询</label>
				<div class="layui-input-inline layui-form">
					<input class="layui-input" name="key" id="key" placeholder="请输入商品名称">
				</div>
			</div>
        </div>
		<div class="layui-form layui-form-pane" style="float:left;width:460px">
			<div class="layui-form-item">
				<label class="layui-form-label">添加时间</label>
				<div style="width:72%" class="layui-input-inline layui-form">
					<input style="width:100%" type="text" class="layui-input" id="created_at" placeholder="请选择时间">
				</div>
			</div>	
		</div>
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">状态</label>
				<div class="layui-input-inline layui-form">
					<select id="status_d">
						<option value="">请选择</option>
						@foreach($status as $key=>$value)
							<option value="{{$key}}">{{$value}}</option>
						@endforeach
					</select>
				</div>
			</div>		
		</div>
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">精品推荐</label>
				<div class="layui-input-inline layui-form">
					<select id="recommend_status_d">
						<option value="">请选择</option>
						@foreach($recommend_status as $key=>$value)
							<option value="{{$key}}">{{$value}}</option>
						@endforeach
					</select>
				</div>
			</div>		
		</div>
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">热卖单品</label>
				<div class="layui-input-inline layui-form">
					<select id="best_status_d">
						<option value="">请选择</option>
						@foreach($best_status as $key=>$value)
							<option value="{{$key}}">{{$value}}</option>
						@endforeach
					</select>
				</div>
			</div>		
		</div>
    </div>
	<div class="demoTable layui-row">
		<button class="layui-btn" style="margin-left:30px;margin-top:15px;" id="search" data-type="reload">查询</button>
		<a href="{{route('shop_admin.product.index')}}" style="margin-top:15px;" class="layui-btn">重置</a>
	</div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
@include('admin::layouts.footer')
<script type="text/html" id="barDemo">
    <a href="/shop_admin/product/@{{d.id}}/edit" class="layui-btn layui-btn-xs">编辑</a>
    <a href="{{route('shop_admin.product.comments_index')}}?id=@{{d.id}}" class="layui-btn layui-btn-xs">评论（@{{d.comments_count}}）</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script> 
<script type="text/html" id="status">
	<input type="checkbox" name="status" value="@{{d.id}}" lay-skin="switch" lay-text="上架|下架" lay-filter="status" @{{ d.status == 1 ? 'checked' : '' }}> 
</script>
<script type="text/html" id="recommend_status"> 
	<input type="checkbox" name="recommend_status" value="@{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="recommend_status" @{{ d.recommend_status == 1 ? 'checked' : '' }}> 
</script>
<script type="text/html" id="best_status">
	<input type="checkbox" name="best_status" value="@{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="best_status" @{{ d.best_status == 1 ? 'checked' : '' }}> 
</script>
<script type="text/html" id="sort">
    <input name="@{{d.id}}" data-id="@{{d.id}}" class="list_order layui-input" value=" @{{d.sort}}" size="10"/>
</script>
<script type="text/html" id="image">
		<a class="example-image-link" href="@{{d.image}}" data-lightbox="example-set" data-title="">
		<img src="@{{d.image}}" style="width:50px;height:100%"/></a>
</script>
<script type="text/html" id="topBtn">
   <a href="{{route('shop_admin.product.create')}}" class="layui-btn layui-btn-sm">添加</a>
</script>
<script>
    layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form,$ = layui.jquery;
        var tableIn = table.render({
			id: 'ad',
            elem: '#list',
            url: "{{route('shop_admin.product.data')}}",
            method:'get',
			toolbar: '#topBtn',
			title:'商品管理',
			page: true,
            cols: [[
                {field:'id', title: '编号', width:100,align: 'center',fixed: true,sort: true}
				,{field:'type_title', title: '商品分类',align: 'center', width:200}
				,{field:'name', title: '商品名称',align: 'center', width:200}
                ,{field:'image', title: '图片',align: 'center', width:200, toolbar: '#image'}
                ,{field:'stock', title: '库存',align: 'center', width:200,sort: true}
                ,{field:'sales', title: '销量',align: 'center', width:200,sort: true}
                ,{field:'created_at', title: '添加时间',align: 'center', width:200,sort: true}
                ,{field:'status', title: '状态',width:150,align: 'center',toolbar: '#status'}
                ,{field:'recommend_status', title: '精品推荐',width:150,align: 'center',toolbar: '#recommend_status'}
                ,{field:'best_status', title: '热卖单品',width:150,align: 'center',toolbar: '#best_status'}
				,{field: 'sort',align: 'center', title: '排序', width: 80, templet: '#sort'}
                ,{width:300, title: '操作',align:'center',align: 'center', toolbar: '#barDemo'} 
            ]],
			limit: "{!! config('admin.common_limit') !!}"
        });
		//搜索
        $('#search').on('click', function () {
			var created_at 			= $('#created_at').val();
			var status  			= $('#status_d').val();
			var key  				= $('#key').val();
			var recommend_status	= $('#recommend_status_d').val();
			var best_status			= $('#best_status_d').val();
            if ($.trim(created_at) === '' && $.trim(status) === '' && $.trim(key) === '' && $.trim(recommend_status) === '' && $.trim(best_status) === ''){
                layer.msg("{!! config('admin.query_condition') !!}", {icon: 0});
                return;
            }
			tableIn.reload({
				where: {created_at:created_at,status:status,key:key,recommend_status:recommend_status,best_status:best_status}
			});
        });
        form.on('switch(status)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var status = obj.elem.checked===true?1:0;
            $.post('/shop_admin/product/'+id+'/status',{_method:'PUT',status:status},function (res) {
				layer.close(loading);
				if (res.status == 1) {  
						layer.msg(res.msg,{time:1000,icon:1});
				}else{
				   layer.msg(res.error,{time:1000,icon:2},function(){
						location.href = res.url;
						});
					return false;
				}
            })
        });
		form.on('switch(recommend_status)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var recommend_status = obj.elem.checked===true?1:0;
            $.post('/shop_admin/product/'+id+'/recommend_status',{_method:'PUT',recommend_status:recommend_status},function (res) {
				layer.close(loading);
				if (res.status == 1) {  
						layer.msg(res.msg,{time:1000,icon:1});
				}else{
				   layer.msg(res.error,{time:1000,icon:2},function(){
						location.href = res.url;
						});
					return false;
				}
            })
        });
		form.on('switch(best_status)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var best_status = obj.elem.checked===true?1:0;
            $.post('/shop_admin/product/'+id+'/best_status',{_method:'PUT',best_status:best_status},function (res) {
				layer.close(loading);
				if (res.status == 1) {  
						layer.msg(res.msg,{time:1000,icon:1});
				}else{
				   layer.msg(res.error,{time:1000,icon:2},function(){
						location.href = res.url;
						});
					return false;
				}
            })
        });
		$('body').on('blur','.list_order',function() {
		   loading =layer.load(1, {shade: [0.1,'#fff']}); 
           var id = $(this).attr('data-id');
           var sort = $(this).val();
           $.post('/shop_admin/product/'+id+'/sorts',{_method:'PUT',sort:sort},function(res){
                layer.close(loading);
				if (res.status == 1) {  
						layer.msg(res.msg,{time:1000,icon:1});
				}else{
				   layer.msg(res.error,{time:1000,icon:2},function(){
						location.href = res.url;
						});
					return false;
				}
           })
        });
    }); 
</script>
@include('admin::layouts.delete_ajax',['url'=>route('shop_admin.product.destroy')])
</body>
</html>